﻿(function QDistrictBox(window, undefined){
	
	/**
	 * 保存window对象
	 */
	var document = window.document,
		_QDistrictBox = window.QDistrictBox,
		QDistrictBox = {},
		List = [];;
	
	window.QDistrictBox = QDistrictBox;
	
	/**
	 * 配置
	 */
	QDistrictBox.option = {
		type : "listbox",
		data : {
			"福建" : {
				"福州市" : ["长乐市", "平潭县", "连江县", "罗源县", "永泰县", "闽清县"
				         ,"台江区", "鼓楼区", "晋安区", "仓山区", "马尾区", "福清市"
				         ,"闽侯县"],
				"厦门市" : ["思明区", "湖里区", "翔安区", "海沧区", "集美区", "同安区"],
				"泉州市" : ["石狮市", "南安市", "惠安县", "安溪县", "德化县", "永春县"
				         ,"泉港区", "金门县", "洛江区", "鲤城区", "丰泽区", "晋江市"]
			},
			"江苏" : {
				"南京市" : ["江宁区", "高淳区", "六合区", "溧水区", "玄武区", "秦淮区"
				         ,"建邺区", "鼓楼区", "栖霞区", "雨花台区", "浦口区"],
				"徐州市" : ["铜山区", "睢宁县", "沛县", "丰县", "贾汪区", "金山桥开发区"
				         ,"铜山经济技术开发区", "八段工业园区", "鼓楼区", "邳州市", "泉山区"
				         ,"新沂市", "云龙区"]
			},
			"广东" : {
				"深圳市" : ["南山区", "罗湖区", "福田区", "宝安区", "光明新区", "坪山新区"
				         ,"大鹏新区", "龙岗区", "盐田区", "龙华新区"]
			}
		},
		provinceCss : {
			"width" : "90px",
			"float" : "left"
		},
		cityCss : {
			"width" : "90px",
			"float" : "left"
		},
		countyCss : {
			"width" : "90px",
			"float" : "left",
			"margin-right" : 0
		}
	}
	
	function districtBox(_DOM, _option){
		
		var $DOM = _DOM || $("<div>"),	//主键
			option = $.extend(true, {}, QDistrictBox.option),	// 深层复制配置
			provinceList,
			cityList,
			countyList;
		
		if(_option) option = $.extend(true, option, _option);
		
		if(option.type == "listbox"){
			
			provinceList = QListBox.init("<div>", {defaultItem:{text:"请选择",value:-1}});
			provinceList.getDOM().css(option.provinceCss);
			provinceList.onSelected(function(_value){
				
				var province = provinceList.getSelectedItem().value,
					tmpData = option.data[province],
					list = [];
				
				for(var i in tmpData){
					list.push(i);
				}
				
				cityList.clear();
				countyList.clear();
				cityList.refresh(list);
				
				var width1 = cityList.getDOM().width();
				cityList.getDOM().css("width", "");
				var width2 = cityList.getDOM().width();
				width1 > width2 ? cityList.getDOM().width(option.cityCss.width) :
					cityList.getDOM().width(width2);
			});
			
			cityList = QListBox.init("<div>", {defaultItem:{text:"请选择",value:-1}});
			cityList.getDOM().css(option.cityCss);
			cityList.onSelected(function(_value){
				
				var province = provinceList.getValue(),
					city = cityList.getValue(),
					tmpData = option.data[province],
					list = [];
				
				if(tmpData){
					list = tmpData[city];
				}
			
				countyList.clear();
				countyList.refresh(list);
				
				var width1 = countyList.getDOM().width();
				countyList.getDOM().css("width", "");
				var width2 = countyList.getDOM().width();
				width1 > width2 ? countyList.getDOM().width(option.countyCss.width) :
					countyList.getDOM().width(width2);
			});
			
			
			countyList = QListBox.init("<div>", {defaultItem:{text:"请选择",value:-1}});
			countyList.getDOM().css(option.countyCss);
			
			$DOM.append(provinceList.getDOM());
			$DOM.append(cityList.getDOM());
			$DOM.append(countyList.getDOM());
		}
		
		this.clear = function(){
			provinceList.clear();
			cityList.clear();
			countyList.clear();
		}
		
		this.refresh = function(_data){
			
			if(!_data) return this;
			
			this.clear();
			var list = [];
			for(var i in _data){
				list.push(i);
			}
			
			provinceList.refresh(list);
		}
		
		this.setValue = function(_value, _regex){
			
			if(!_value) return;
			
			var strs = _value.split(_regex || ";");
			provinceList.setValue(strs[0]);
			cityList.setValue(strs[1]);
			countyList.setValue(strs[2]);
		}
		
		this.getValue = function(_regex){
			
			var tmpArray = [],
				province = provinceList.getValue(),
				city = cityList.getValue(),
				county = countyList.getValue();
			
			if(province == -1 && city == -1 && county == -1) return -1;
			
			tmpArray.push(province);
			tmpArray.push(city);
			tmpArray.push(county);
			return tmpArray.join(_regex || ";");
		}
	};
	
	QDistrictBox.init = function(_pk, _option){
		
		var $DOM = $(_pk);							//传入对象可以是jquery对象，或者是表达式
		var box = new districtBox($DOM, _option);
		List[_pk] = List.length;				//索引，其实没必要，直接用对象的map性质存储也是可以的，这里为了让代码更好理解
		List.push(box);							//保存创建的对象
		return box;						//初始化完成，返回对象
	}
	
	/**
	 * 获取tip
	 */
	QDistrictBox.get = function(_pk){
		
		return List[List[_pk]];
	}
})(window);